<template>
	<view>
		<uni-nav-bar left-icon="back" left-text="返回" fixed="true" statusBar="true"  @clickLeft="leftAction" title="终端管理" right-text="新增"
			@clickRight="addAction"></uni-nav-bar>
		<view class="uni-goonbtn-v">
			<button type="default" @click="debugAction">查询终端注册信息列表</button>
		</view>
		<view v-if="dataList.length>0" class="uni-content-v">
			<uni-section :title='title+"("+form.baseStationId+")"' type="line"></uni-section>
			<uni-collapse ref="add" class="warp" @change="change">
				<uni-collapse-item v-for="(sub, key) in dataList" :key="key" :open="sub.open" :title="sub.name">
					<uni-list>
						<template v-for="(item,index) in sub.dataArr">
							<uni-list-item :note="item.subname" :rightText="item.value" />
						</template>
					</uni-list>

					<view class="uni-flex uni-row">
						<button class="flex-item button" type="default" @click="dataListAction(key)">数据</button>
						<button class="flex-item button" type="default" @click="alarmAction(key)">报警</button>
						<button class="flex-item button" type="default" @click="editAction(key)">编辑</button>
						<button class="flex-item button" type="default" @click="deleteAction(key)">删除</button>
					</view>
				</uni-collapse-item>
			</uni-collapse>
		</view>

	</view>
</template>

<script>
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue'
	import wsconnect from '@/api/webservice.js'

	export default {
		data() {
			return {
				dataList: [],
			};
		},
		onLoad(options) {
			this.title = options.title
			this.bs_id = options.id
			this.$http.get('/netty/station/' + this.bs_id).then(res => {
				const form = res.data.data
				this.form = form
			}).catch(err => {
				uni.showModal({
					content: '出错了，请稍后再试',
					showCancel: false
				})
			})
			uni.hideLoading()

		},
		components: {
			uniNavBar
		},
		methods: {
			change(e) {},
			leftAction() {
				uni.navigateBack({
					delta: 1
				});
			},
			addAction() {
				uni.navigateTo({
					url: "../clientAdd/clientAdd?s=" + this.form.sendCmdTagName + '&r=' + this.form.reqTagName +
						'&id=' + this.form.baseStationId
				})
			},
			editAction(index) {
				const data = this.dataList[index]
				const clientId = data.dataArr[0].value
				console.log(clientId)
				uni.navigateTo({
					url: "../clientConfig/clientConfig?clientId=" + clientId +'&id=' +this.bs_id
				})
				
			},
			dataListAction(index) {
				const data = this.dataList[index]
				const clientId = data.dataArr[0].value
				console.log(clientId)
				uni.navigateTo({
					url: "../clientDataList/clientDataList?clientId=" + clientId
				})
			},
			alarmAction(index) {
				const data = this.dataList[index]
				const clientId = data.dataArr[0].value
				console.log(clientId)
				uni.navigateTo({
					url: "../alarmDataList/alarmDataList?clientId=" + clientId
				})
			},
			deleteAction(index) {
				const data = this.dataList[index]
				const clientId = data.dataArr[0].value
				console.log(clientId)
				uni.showModal({
					title: "是否要删除终端？",
					showCancel: true,
					success: res => {
						if (res.confirm) {

							wsconnect.requestJson({
								items: [sendCmdTagName + '.DESC'],
								vals: ['TerDelRq,/TI='+clientId+'/'],
								type: "SetDataByName",
							}, clientListTagName).then(res => {
								uni.showModal({
									content:"删除成功",
									showCancel:false
								})
								this.debugAction()
								wsconnect.clearReqData(1, clientListTagName)
							}).catch(error => {
								if (error.code == 1) {
									uni.redirectTo({
										url: "../index/index"
									})
								} else {
									uni.showModal({
										content: error.msg,
										showCancel: false
									})
								}
							})
						}

					}
				})
			},
			debugAction() {
				const sendCmdTagName = this.form.sendCmdTagName
				const clientListTagName = this.form.clientListTagName

				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: ['TerRegListInqRq,//'],
					type: "SetDataByName",
				}, clientListTagName).then(res => {
					this.configDataList(res)
					wsconnect.clearReqData(1, clientListTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
			},
			configDataList(res) {
				var arr = res.replace(/\r\n/g, "").split(',/')
				var result = arr[1]
				result = result.slice(1, result.length - 4)
				arr = result.split('><')

				var a = []
				//每个基站
				for (let item of arr) {
					var dic = {}
					const item_arr = item.split('/')
					for (let model of item_arr) {
						const model_arr = model.split('=')
						dic[model_arr[0]] = model_arr[1]
					}
					a.push(dic)
				}
				var data_list = []
				for (let item of a) {

					//发射功率
					const tp = item['TP']
					const tp_arr = ["0dBm", "2dBm", "5dBm", "7dBm", "10dBm", "14dBm", "17dBm", "22dBm"]
					const tp_str = tp_arr[tp - 1]

					//空中唤醒
					const dc = item['DC']
					const dc_arr = ["休眠2s/接收30ms", "休眠2s/接收40ms", "休眠3s/接收40ms", "休眠4s/接收40ms", "休眠5s/接收40ms"]
					const dc_str = dc_arr[dc - 1]

					//采样间隔
					const sp = item['SP']
					const sp_arr = ['1s', '5s', '10s', '30s', '1min', '3min', '5min', '10min', '15min', '30min', '1h']
					const sp_str = sp_arr[sp - 1]

					//上传间隔
					const ut = item['UT']
					const ut_arr = ['1s', '5s', '30s', '1min', '3min', '5min', '10min', '15min', '30min', '1h', '2h']
					const ut_str = ut_arr[ut - 1]
					data_list.push({
						name: "终端名:" + item['TI'],
						dataArr: [{
								subname: "终端ID",
								value: item['TI'],
								key: 'TI'
							},
							{
								subname: "类型",
								value: item['TY'] == 1 ? 'PT100' : '压力计',
								key: 'TY'
							},
							{
								subname: "配置通道",
								value: item['CH'],
								key: 'CH'
							},
							{
								subname: "数据通道",
								value: item['DH'],
								key: 'DH'
							},
							{
								subname: "报警通道",
								value: item['AH'],
								key: 'AH'
							},
							{
								subname: "发射功率",
								value: tp_str,
								key: 'TP'
							},
							{
								subname: "空中唤醒",
								value: dc_str,
								key: 'DC'
							},
							{
								subname: "采样间隔",
								value: sp_str,
								key: 'SP'
							},
							{
								subname: "上传间隔",
								value: ut_str,
								key: 'UT'
							},
							{
								subname: "报警上限",
								value: item['HT'],
								key: 'HT'
							},
							{
								subname: "报警下限",
								value: item['LT'],
								key: 'LT'
							},
							{
								subname: "校准",
								value: item['CV'],
								key: 'CV'
							},
							{
								subname: "电量",
								value: item['BV'],
								key: 'BV'
							},
							{
								subname: "RSSI",
								value: item['RS'],
								key: 'RS'
							},
							{
								subname: "SN",
								value: item['SN'],
								key: 'SN'
							},
						]

					})
				}
				this.dataList = data_list
				console.log(data_list)
			}
		}
	}
</script>

<style lang="scss">
	@import '@/common/uni-nvue.scss';

	.uni-content-v {
		margin-top: 100rpx;
	}

	.uni-goonbtn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		margin-top: 40rpx;
		height: 40rpx;
	}

	.content {
		padding: 15px;
		font-size: 14px;
		line-height: 20px;
		background-color: #f9f9f9;
		color: #666;
	}

	.uni-row {
		display: flex;
		background-color: #ffffff;
		flex-direction: row;
	}

	.button {
		background-color: #ffffff;
		margin-left: 0rpx;
		margin-right: 0rpx;
		width: 100%;
		border: none;
	}

	.button::after {
		border-radius: 0px;
	}
</style>
